#include "Analysis.h"

#include <fstream>
#include <iostream>

using namespace std;

Analysis::Analysis()
{
    //ctor
}

Analysis::~Analysis()
{
    //dtor
}

void Analysis::print_results(vector<Assoc> &assocs, const char * filename)
{
    ofstream out;
    out.open(filename);
    for (int i = 0; i < assocs.size(); i++)
    {
        out << assocs[i].get_spacer_header() << '\t' << assocs[i].get_virus_header() << '\t' << assocs[i].get_long_loc() << '\t' << assocs[i].get_ident();
        if (i < assocs.size()-1)
        {
            out << endl;
        }
    }
}

void Analysis::make_cytoscape(vector<Assoc> &assocs, const char * filename)
{
    ofstream out;
    out.open(filename);
    if (out.is_open()==false)
    {
        cout << "output file not opened" << endl;
    }
    for (int i = 0; i < assocs.size(); i++)
    {
        out << assocs[i].get_spacer_header() + '\t' + "sv" + '\t';
        out << assocs[i].get_virus_header() << '\t';
        out << assocs[i].get_ident() << endl;
    }
    out.clear();
    out.close();
}

void Analysis::find_PAMs(vector<Assoc> &assocs, const char * filename)
{
    ofstream out;
    out.open(filename);
    for (int i =0; i < assocs.size(); i++)
    {
        string virus_seq = assocs[i].get_virus_seq();
        string spacer_header = assocs[i].get_spacer_header();
        string virus_header = assocs[i].get_virus_header();
        int spacer_length = assocs[i].get_spacer_length();
        int index = assocs[i].get_long_loc();
        float ident = assocs[i].get_ident();
        if (virus_seq=="0")
        {
            cout << "virus not found" << endl;
        }
        //motifs from http://www.landesbioscience.com/journals/rnabiology/2012RNABIOL0286R.pdf
        //-NGG
        if (virus_seq[index+spacer_length+2]=='G' && virus_seq[index+spacer_length+3]=='G')
        {
            out << spacer_header << '\t' << virus_header << '\t' << index<< '\t' << ident << '\t' << "-NGG" << '\t' << virus_seq.substr(index + spacer_length+1, 10) <<endl;
        }
        //-GG
        if (virus_seq[index+spacer_length+1]=='G' && virus_seq[index+spacer_length+2]=='G')
        {
            out << spacer_header << '\t' << virus_header << '\t' << index<< '\t' << ident << '\t' << "-GG" << '\t' << virus_seq.substr(index + spacer_length+1, 10) <<endl;
        }
        //-GAA
        if (virus_seq[index+spacer_length+1]=='G' && virus_seq[index+spacer_length+2]=='A' && virus_seq[index+spacer_length+3]=='A')
        {
            out << spacer_header << '\t' << virus_header << '\t' << index<< '\t' << ident << '\t' << "-GAA" << '\t' << virus_seq.substr(index + spacer_length+1, 10) <<endl;
        }
        //-AGT
        if (virus_seq[index+spacer_length+1]=='A' && virus_seq[index+spacer_length+2]=='G' && virus_seq[index+spacer_length+3]=='T')
        {
            out << spacer_header << '\t' << virus_header << '\t' << index<< '\t' << ident << '\t' << "-AGT" << '\t' << virus_seq.substr(index + spacer_length+1, 10) <<endl;
        }
        //-TTA
        if (virus_seq[index+spacer_length+1]=='T' && virus_seq[index+spacer_length+2]=='T' && virus_seq[index+spacer_length+3]=='A')
        {
            out << spacer_header << '\t' << virus_header << '\t' << index<< '\t' << ident << '\t' << "-TTA" << '\t' << virus_seq.substr(index + spacer_length+1, 10) <<endl;
        }
        //-ATA
        if (virus_seq[index+spacer_length+1]=='A' && virus_seq[index+spacer_length+2]=='T' && virus_seq[index+spacer_length+3]=='A')
        {
            out << spacer_header << '\t' << virus_header << '\t' << index<< '\t' << ident << '\t' << "-ATA" << '\t' << virus_seq.substr(index + spacer_length+1, 10) <<endl;
        }
        //-CTA
        if (virus_seq[index+spacer_length+1]=='C' && virus_seq[index+spacer_length+2]=='T' && virus_seq[index+spacer_length+3]=='A')
        {
            out << spacer_header << '\t' << virus_header << '\t' << index<< '\t' << ident << '\t' << "-CTA" << '\t' << virus_seq.substr(index + spacer_length+1, 10) <<endl;
        }
        //-GTG
        if (virus_seq[index+spacer_length+1]=='G' && virus_seq[index+spacer_length+2]=='T' && virus_seq[index+spacer_length+3]=='G')
        {
            out << spacer_header << '\t' << virus_header << '\t' << index<< '\t' << ident << '\t' << "-GTG" << '\t' << virus_seq.substr(index + spacer_length+1, 10) <<endl;
        }
        //-CTT
        if (virus_seq[index+spacer_length+1]=='C' && virus_seq[index+spacer_length+2]=='T' && virus_seq[index+spacer_length+3]=='T')
        {
            out << spacer_header << '\t' << virus_header << '\t' << index<< '\t' << ident << '\t' << "-CTT" << '\t' << virus_seq.substr(index + spacer_length+1, 10) <<endl;
        }
        //-CAT
        if (virus_seq[index+spacer_length+1]=='C' && virus_seq[index+spacer_length+2]=='A' && virus_seq[index+spacer_length+3]=='T')
        {
            out << spacer_header << '\t' << virus_header << '\t' << index<< '\t' << ident << '\t' << "-CAT" << '\t' << virus_seq.substr(index + spacer_length+1, 10) <<endl;
        }
        //-CCT
        if (virus_seq[index+spacer_length+1]=='C' && virus_seq[index+spacer_length+2]=='C' && virus_seq[index+spacer_length+3]=='T')
        {
            out << spacer_header << '\t' << virus_header << '\t' << index<< '\t' << ident << '\t' << "-CCT" << '\t' << virus_seq.substr(index + spacer_length+1, 10) <<endl;
        }
        //-CTC
        if (virus_seq[index+spacer_length+1]=='C' && virus_seq[index+spacer_length+2]=='T' && virus_seq[index+spacer_length+3]=='C')
        {
            out << spacer_header << '\t' << virus_header << '\t' << index<< '\t' << ident << '\t' << "-CTC" << '\t' << virus_seq.substr(index + spacer_length+1, 10) <<endl;
        }
        //CCN-
        if (virus_seq[index-2]=='C' && virus_seq[index-3]=='C')
        {
            out << spacer_header << '\t' << virus_header << '\t' << index<< '\t' << ident << '\t' << "CCN-" << '\t' << virus_seq.substr(index-10, 10) <<endl;
        }
        //CNCCN-
        if (virus_seq[index-2]=='C' && virus_seq[index-3]=='C' && virus_seq[index-5]=='C')
        {
            out << spacer_header << '\t' << virus_header << '\t' << index<< '\t' << ident << '\t' << "CNCCN-" << '\t' << virus_seq.substr(index-10, 10) <<endl;
        }
        //WTTCTNN-
        if (virus_seq[index-3]=='T' && virus_seq[index-4]=='C' && virus_seq[index-5]=='T' && virus_seq[index-6]=='T' && virus_seq[index-7]==('A'^'T'))
        {
            out << spacer_header << '\t' << virus_header << '\t' << index<< '\t' << ident << '\t' << "WTTCTNN-" << '\t' << virus_seq.substr(index-10, 10) <<endl;
        }
        //TTYRNNN-
        if (virus_seq[index-3]=='T' && virus_seq[index-4]=='C' && virus_seq[index-5]=='T' && virus_seq[index-6]=='T' && virus_seq[index-7]==('A'^'T'))
        {
            out << spacer_header << '\t' << virus_header << '\t' << index<< '\t' << ident << '\t' << "TTYRNNN-" << '\t' << virus_seq.substr(index-10, 10) <<endl;
        }
    }
}
